package cn.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import cn.hr.pojo.Organization;
import cn.hr.pojo.SalaryPro;
import cn.hr.pojo.SysUser;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hr.service.SalaryProService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 薪酬项目控制器
 * @author MO
 *
 */
@Controller
@RequestMapping("/sys/salary")
public class SalaryProController {
    private Logger logger = LoggerFactory.getLogger(SalaryProController.class);
    @Resource
    private SalaryProService salaryProService;

    private String meagess;

    /**
     * 查询
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        HashMap<String,Object> map = new HashMap<>();
        try {
            PageInfo<Map> pageInfo = salaryProService.findListByCondition(map,0);
            model.addAttribute("pageInfo",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "salaryPro/salaryProList";
    }

    /**
     * 添加
     * @param session
     * @return
     */
    @RequestMapping("/addSave")
    public String addSave(SalaryPro salaryPro, HttpSession session) {
        salaryPro.setCreatedById(((SysUser) (session.getAttribute(Constants.USER_SESSION))).getId());
        salaryPro.setCreatedDate(new Date());
        if (salaryPro.getIsStart()==null){
            salaryPro.setIsStart(0);
        }
        try {
            salaryProService.insert(salaryPro);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/sys/salary/list";
    }


    /**
     * 修改
     * @param session
     * @return
     */
    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(SalaryPro salaryPro, HttpSession session){
        salaryPro.setModifyById(((SysUser) (session.getAttribute(Constants.USER_SESSION))).getId());
        salaryPro.setModifyDate(new Date());
        System.out.print(salaryPro.getCode());
        try {
            meagess = "false";
            if(salaryProService.update(salaryPro)) {
                meagess = "true";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return meagess;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam(value = "id")String id){
        try {
            meagess = salaryProService.deleteById(Integer.parseInt(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  meagess;
    }

    @RequestMapping("/code")
    @ResponseBody
    public String getCode(){
        String code = "SALARYPRO_";
        try {
            code = code+(salaryProService.findMin()+1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return code;
    }
}
